package cn.cmxds.store.view.Impl.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.cmxds.store.R;
import cn.cmxds.store.bean.Tab;
import cn.cmxds.store.view.Impl.fragment.CartFragmentView;
import cn.cmxds.store.view.Impl.fragment.HomeViewFragment;
import cn.cmxds.store.view.Impl.fragment.HotFragmentView;
import cn.cmxds.store.view.Impl.fragment.MineFragmentView;
import cn.cmxds.store.widget.FragmentTabHost;

public class MainActivity extends AppCompatActivity {
    private LayoutInflater mInflater;

    private FragmentTabHost mTabhost;

    private CartFragmentView cartFragmentView;

    private List<Tab> mTabs = new ArrayList<>(5);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTab();
    }
    private void initTab() {


        Tab tab_home = new Tab(HomeViewFragment.class,R.string.home,R.drawable.selector_icon_home);
        Tab tab_hot = new Tab(HotFragmentView.class,R.string.hot,R.drawable.selector_icon_hot);
//        Tab tab_msg = new Tab(MsgFragmentView.class,R.string.msg,R.drawable.selector_icon_msg);
        Tab tab_cart = new Tab(CartFragmentView.class,R.string.cart,R.drawable.selector_icon_cart);
        Tab tab_mine = new Tab(MineFragmentView.class,R.string.mine,R.drawable.selector_icon_mine);
        mTabs.add(tab_home);
        mTabs.add(tab_hot);
//        mTabs.add(tab_msg);
        mTabs.add(tab_cart);
        mTabs.add(tab_mine);
        mInflater = LayoutInflater.from(this);
        mTabhost = this.findViewById(android.R.id.tabhost);
        mTabhost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);

        for (Tab tab : mTabs){

            TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));

            tabSpec.setIndicator(buildIndicator(tab));

            mTabhost.addTab(tabSpec,tab.getFragment(),null);

        }

    }

    private View buildIndicator(Tab tab) {
        View view =mInflater.inflate(R.layout.tab_indicator,null);
        ImageView img = view.findViewById(R.id.icon_tab);
        TextView text = view.findViewById(R.id.txt_indicator);

        img.setBackgroundResource(tab.getIcon());
        text.setText(tab.getTitle());

        return  view;
    }
}
